/******************************************************************************

  Copyright (C), 2012-2016, ZHANGYA

 ******************************************************************************
  File Name     : delay.h
  Version       : Initial Draft
  Author        : zhangya
  Created       : 2023/7/14
  Last Modified :
  Description   : delay.h
  Function List :
  History       :
  1.Date        : 2023/7/14
    Author      : zhangya
    Modification: Created file

******************************************************************************/
#ifndef _DELAY_H_
#define _DELAY_H_

#include "ngx_rbtree.h"
#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

/*--------------------------------------------------------------------*
 * $external variables                           *
 *--------------------------------------------------------------------*/
struct timer_node_s;

typedef void (*timer_handler_pt)(struct timer_node_s *);

typedef struct timer_node_s{
    ngx_rbtree_node_t node;
    timer_handler_pt  handler;
}timer_node_t;

#define TIMER_NODE_MAX		(8)

#define TIMEROUT_SECOND(_s)		((_s) * 1000)
#define TIMEROUT_MINUTE(_m)		((_m) * 1000 * 60)

/*--------------------------------------------------------------------*
 * $external routine prototypes                  *
 *--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*
 * $internal routine prototypes                  *
 *--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*
 * $project-wide global variables                *
 *--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*
 * $module-wide global variables                 *
 *--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*
 * $constants                                    *
 *--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*
 * $macros                                       *
 *--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*
 * $routines' implementations                    *
 *--------------------------------------------------------------------*/

void init_timer(void);

timer_node_t *add_timer(unsigned int expire, timer_handler_pt func);

void del_timer(timer_node_t * tn);

void timer_handle(void);

void delay_ms(unsigned int ms);

void delay_us(unsigned int us);

unsigned int get_system_cnt(void);

bool system_timeout(unsigned int * tick, unsigned int timeout);


#ifdef __cplusplus
}
#endif


#endif //#ifndef _DELAY_H_

